#include <stdio.h>

#include "logcplus_common.h"
#include "logcplus_layout.h"
#include "logcplus_writer_console.h"

namespace logcplus
{
	#define maxBufferSize       8192

	logcplus_writer_console::logcplus_writer_console()
		: logcplus_base_writer(logcplus_consoleAppender)
	{
		m_status = true;
	}

	logcplus_writer_console::~logcplus_writer_console()
	{
		LogUInit();
	}

	int  logcplus_writer_console::LogInit()
	{
		m_status = true;
		return 0;
	};

	int  logcplus_writer_console::LogInit(const string& sLogName, int nLogLevel,const string& sLogNode)
	{
		m_logLevel = (logcplus_level_t)nLogLevel;
		return LogInit();
	};

	int  logcplus_writer_console::LogUInit()
	{
		m_status = false;
		return 0;
	};

	int  logcplus_writer_console::LogBegin(uint64_t uLogSeqno)
	{
		return 0;
	};

	void logcplus_writer_console::LogCommit()
	{
		fflush(stdout);
	};

	void logcplus_writer_console::LogWrite(const char* File, const char* Function, int Line,  int Level, const char* LogMsg)
	{
		char sLogText[4096]={0},sLogFmtBuf[1024]={0};
		logcplus_layout logLayout(m_layout.c_str());
		logLayout.formatLayout(File,Function,Line,Level,sLogFmtBuf);
		snprintf(sLogText,sizeof(sLogText)-1,"%s %s\n",sLogFmtBuf,LogMsg);
		LogWrite(sLogText,Level);    
	};

	void logcplus_writer_console::LogWrite(const char* sLogText,int nFlag)
	{
		switch (nFlag)
		{
		case LOGCPLUS_TRACE : 
			printf( COLOR_DARY_GRAY "%s" COLOR_NONE,sLogText);
			break;
		case LOGCPLUS_DEBUG : 
			printf( COLOR_DARY_GRAY "%s" COLOR_NONE,sLogText);
			break;
		case LOGCPLUS_INFO  : 
			printf( COLOR_NONE "%s" COLOR_NONE,sLogText);
			break;
		case LOGCPLUS_WARN  : 
			printf( COLOR_YELLOW "%s" COLOR_NONE,sLogText);
			break;
		case LOGCPLUS_ERROR : 
			printf( COLOR_RED "%s" COLOR_NONE,sLogText);
			break;
		case LOGCPLUS_FATAL : 
			printf( COLOR_LIGHT_RED "%s" COLOR_NONE,sLogText);
			break;			
		case LOGCPLUS_UNKOWN:
		default: 
			printf( COLOR_NONE "%s" COLOR_NONE,sLogText);
			break;;
		}		
	};

	void logcplus_writer_console::Trace(  const char* File, const char* Function, int Line,  const char* Fmt, ...)
	{
		if(!m_status) return;
		WRITE_LOG(LOGCPLUS_TRACE);    
	};

	void logcplus_writer_console::Debug(  const char* File, const char* Function, int Line,  const char* Fmt, ...)
	{
		if(!m_status) return;
		WRITE_LOG(LOGCPLUS_DEBUG);    
	};

	void logcplus_writer_console::Info(   const char* File, const char* Function, int Line,  const char* Fmt, ...)
	{
		if(!m_status) return;
		WRITE_LOG(LOGCPLUS_INFO);       
	};

	void logcplus_writer_console::Warn(   const char* File, const char* Function, int Line,  const char* Fmt, ...)
	{
		if(!m_status) return;
		WRITE_LOG(LOGCPLUS_WARN);      
	};

	void logcplus_writer_console::Error(  const char* File, const char* Function, int Line,  const char* Fmt, ...)
	{
		if(!m_status) return;
		WRITE_LOG(LOGCPLUS_ERROR);      
	};

	void logcplus_writer_console::Fatal(  const char* File, const char* Function, int Line,  const char* Fmt, ...)
	{
		if(!m_status) return;
		WRITE_LOG(LOGCPLUS_FATAL);      
	};

	void logcplus_writer_console::LogHex( const char* File, const char* Function, int Line,  int Level, const void* data, size_t dataSize )
	{
		if( !m_status)  { return; }
		if(	!IsEnabledFor(Level) ) { return; }

		char sHexFmtBuf[maxBufferSize]={0};
		char sLogMsg[maxBufferSize]={0};
		char sLogFmtBuf[1024]={0};	

		FormatHex(data,"", dataSize,sHexFmtBuf);
		logcplus_layout logLayout(m_layout.c_str());
		logLayout.formatLayout(File,Function,Line,Level,sLogFmtBuf);
		snprintf(sLogMsg,sizeof(sLogMsg)-1,"%s %s\n",sLogFmtBuf,sHexFmtBuf);
		LogWrite(sLogMsg);    
		return;    
	};

	void logcplus_writer_console::Logger( const char* File, const char* Function, int Line,  int Level, const char* Fmt, ...)
	{
		if(!m_status) return;
		WRITE_LOG(Level);    
	};

	void logcplus_writer_console::WLog(   const char* File, const char* Function, int Line,  int Level, const char* sLogText)
	{
		if(!m_status) return;
		LogWrite(File,Function,Line,Level,sLogText);    
	};
}
